package com.lxp.rename;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class RenameFile {

    //C:\Users\Administrator\AppData\Roaming\360Safe\DesktopRest\eye\bk_image
    final static String FILE_PATH = "D:\\360Downloads\\图画\\temp\\";

    public static void main(String[] args) {
        try {
            readfile(FILE_PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readfile(String filepath) throws FileNotFoundException, IOException {
        try {
            File file = new File(filepath);
            if (!file.isDirectory()) {
                System.out.println("文件");
                System.out.println("path=" + file.getPath());
                System.out.println("absolutepath=" + file.getAbsolutePath());
                System.out.println("name=" + file.getName());
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                System.out.println("文件夹:" + filelist.length + "个文件");
                String suffix = null;
                String prefix = null;
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    System.out.print(i + "---{");
                    if (!readfile.isDirectory()) {
//                         System.out.println("path=" + readfile.getPath());
//                        System.out.println("absolutepath=" + readfile.getAbsolutePath());
                        //修改文件名称
                        fileNenameTo(suffix, prefix, readfile);
                    } else if (readfile.isDirectory()) {
                        readfile(filepath + "\\" + filelist[i]);
                    }
                    System.out.println("}");
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        }
    }

    private static void fileNenameTo(String suffix, String prefix, File readfile) {
//        System.out.print("name=" + readfile.getName()); //文件名称
        String name = readfile.getName();
        //获取名称
        prefix = name.substring(0, name.lastIndexOf(".")) + ".png";
//        prefix = name.substring(name.indexOf("S"), name.lastIndexOf("."));
//        prefix = prefix.replace("尚硅谷_", "");
        //获取文件后缀
//        suffix = name.substring(name.lastIndexOf("."), name.length());
//        File saveFile = new File(FILE_PATH + prefix + suffix);
        File saveFile = new File(FILE_PATH + prefix);
        //重命名
        System.out.print("---" + readfile.renameTo(saveFile));
        System.out.println("[---]" + prefix + "[---]" + suffix);
    }

}
